﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using GameSPK.Content;
using GameSPK.DAO;

namespace GameSPK
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        bool notset = true;
        public static General cuocthiluachon
        {
            get
            {
                return getCuocThiChon();
            }
        }
        public MainWindow()
        {
            InitializeComponent();
            KhoiTaoMenuItemLayout();
            
        }
        public static General getCuocThiChon()
        {
            return CuocThiDAO.getCuocThiChon();
        }
        void KhoiTaoMenuItemLayout()
        {
            DBTroChoiDataContext db = new DBTroChoiDataContext();
            mLayout.Items.Clear();
            
            mContent.Items.Clear();
            MenuItem itemnlayout = new MenuItem();
            itemnlayout.Name = "mmainLayout";
            itemnlayout.Header = "Thiết Lập Cài Đặt Layout";
            itemnlayout.Click += new RoutedEventHandler(MenuItemLayout_Click);
            mLayout.Items.Add(itemnlayout);
            try
            {
                var cuocthi = (from ct in db.Generals
                               where ct.Chon == true
                               select ct).Single();
                var phanchoi = (from pc in db.PhanThi_CuocThis
                                where (pc.MaCuocChoi == cuocthi.MaCuocChoi)
                                select pc

                                );

                if (phanchoi.Count() <= 0)
                {
                    return;
                }
                MenuItem itemnguoichoi = new MenuItem();
                itemnguoichoi.Name = "mnguoichoi";
                itemnguoichoi.Header = "Giới thiệu người chơi";
                itemnguoichoi.Click += new RoutedEventHandler(item_Click);
                mLayout.Items.Add(itemnguoichoi);
                MenuItem itemphanchoi = new MenuItem();
                itemphanchoi.Name = "mphanchoi";
                itemphanchoi.Header = "Giới thiệu phần chơi";
                itemphanchoi.Click += new RoutedEventHandler(item_Click);
                mLayout.Items.Add(itemphanchoi);


                MenuItem itemnhapnguoichoi = new MenuItem();
                itemnhapnguoichoi.Name = "mnguoichoi";
                itemnhapnguoichoi.Header = "Chọn Người Chơi";
                itemnhapnguoichoi.Click += new RoutedEventHandler(itemContent_Click);
                mContent.Items.Add(itemnhapnguoichoi);

                foreach (PhanThi_CuocThi phanthi_ct in phanchoi)
                {
                    var phanchoicon = (from pcc in db.PhanThis
                                       where pcc.MaPT == phanthi_ct.MaPT
                                       select pcc).Single();
                    MenuItem item = new MenuItem();
                    MenuItem itemContent = new MenuItem();
                    try
                    {
                        item.Name = "m" + phanchoicon.MaPT;
                        itemContent.Name = "mContent" + phanchoicon.MaPT;
                    }
                    catch
                    {
                        continue;
                    }

                    item.Header = phanchoicon.TenPT.TrimEnd();
                    itemContent.Header = phanchoicon.TenPT.TrimEnd();
                    item.Click += new RoutedEventHandler(item_Click);
                    itemContent.Click += new RoutedEventHandler(itemContent_Click);
                    mLayout.Items.Add(item);
                    mContent.Items.Add(itemContent);
                    //mainGridPro.Children.Add(item);
                }
                notset = false;
                mCuocthi.IsEnabled = true;
            }
            catch
            {
                notset = true;
            }
        }

        void item_Click(object sender, RoutedEventArgs e)
        {
            MenuItem item = (MenuItem)sender;
            frmSectionGeneralLayout section11;
            if (item.Name == "m1")
            {
                section11 = frmSectionGeneralLayout.getInstance(1);
                section11.Show();
                section11.Focus();
            }
            else if (item.Name == "mnguoichoi")
            {
                GameSPK.Layout.frmGioiThieuNguoiChoi gioithieunguoichoi = Layout.frmGioiThieuNguoiChoi.getInstance();
                gioithieunguoichoi.Show();
                gioithieunguoichoi.Focus();

            }
            else if (item.Name == "mphanchoi")
            {
                GameSPK.Layout.frmHienPhanThi gioithieuphanchoi = Layout.frmHienPhanThi.getInstance();
                gioithieuphanchoi.Show();
                gioithieuphanchoi.Focus();

            }

            else if (item.Name == "m2")
            {
                section11 = frmSectionGeneralLayout.getInstance(2);
                section11.Show();
                section11.Focus();
            }
            else if (item.Name == "m3")
            {
                frmSectionOchuLayout section3 = frmSectionOchuLayout.getInstance();
                section3.Show();
                section3.Focus();
            }
            else if (item.Name == "m4")
            {
                section11 = frmSectionGeneralLayout.getInstance(4);
                section11.Show();
                section11.Focus();
            }
            else if (item.Name == "m5")
            {
                section11 = frmSectionGeneralLayout.getInstance(5);
                section11.Show();
                section11.Focus();
            }
        }

        void itemContent_Click(object sender, RoutedEventArgs e)
        {
            MenuItem item = (MenuItem)sender;
           
            if (item.Name == "mContent1")
            {
                frmSection1Content phan1ND = new frmSection1Content();
                phan1ND.phanthi = 1;
                phan1ND.ShowDialog();
            }
            else if (item.Name == "mnguoichoi")
            {
                frmChonNguoiThi formchonnguoithi = new frmChonNguoiThi();
                formchonnguoithi.ShowDialog();
            }
            else if (item.Name == "mContent2")
            {
                frmSection2Content phan2ND = new frmSection2Content();
                phan2ND.ShowDialog();
            }
            else if (item.Name == "mContent3")
            {
                frmOChuContent phan3ND = new frmOChuContent();
                phan3ND.ShowDialog();
            }
            else if (item.Name == "mContent4")
            {
                GameSPK.Content.frmSection4Content phan4ND = new GameSPK.Content.frmSection4Content();
                phan4ND.ShowDialog();
            }
            else if (item.Name == "mContent5")
            {
                frmSection1Content phan5ND = new frmSection1Content();
                phan5ND.phanthi = 5;
                phan5ND.ShowDialog();
            }
        }
        private void MenuItemLayout_Click(object sender, RoutedEventArgs e)
        {
            frmMainLayout mainLayout = frmMainLayout.getInstance();
            mainLayout.ShowDialog();
            //mainLayout.Focus();
            //if (notset)
            //{
                KhoiTaoMenuItemLayout();
            //}
        }
        //string homebtn;
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            if (CuocThiDAO.getCuocThiChon() == null)
            {
                this.mCuocthi.IsEnabled = false;
            }
        }

        private void mCuocthi_Click(object sender, RoutedEventArgs e)
        {
            if (SetupDAO.isSetupConfig() && SetupDAO.isContentReady(MainWindow.getCuocThiChon()))
            {
                Start.frmModau modau = Start.frmModau.getInstance();
                modau.ShowDialog();
                modau.Focus();
            }
            else
            {
                MessageBox.Show("Not Ready", "Error", MessageBoxButton.OK);
            }

        }

        private void mSetup_Click(object sender, RoutedEventArgs e)
        {
            frmSetup frmsetup = frmSetup.getInstance();
            frmsetup.Show();
            frmsetup.Focus();
            KhoiTaoMenuItemLayout();
        }

        private void mchooseContent_Click(object sender, RoutedEventArgs e)
        {
            frmChonCuocChoi frmChon = frmChonCuocChoi.getInstance(false);
            frmChon.Show();
            frmChon.Focus();
        }

        private void mchooseStart_Click(object sender, RoutedEventArgs e)
        {
            frmChonCuocChoi frmChon = frmChonCuocChoi.getInstance(true);
            frmChon.Show();
            frmChon.Focus();
        }

        private void mFile_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void mHelp_Click(object sender, RoutedEventArgs e)
        {

        }

        private void mCheckLayOut_Click(object sender, RoutedEventArgs e)
        {
            if (SetupDAO.isSetupConfig())
            {
                MessageBox.Show("OK");
            }
            else
            {
                MessageBox.Show("don't config enough","Error",MessageBoxButton.OK);
            }
        }

        private void mCheckContent_Click(object sender, RoutedEventArgs e)
        {
            if (SetupDAO.isContentReady(MainWindow.getCuocThiChon()))
            {
                MessageBox.Show("Ready");
            }
        }
        
       

        
    }
}
